
const app = getApp()
let keyword = ''
const db = wx.cloud.database()
import formatTime from '../../../style/formatTime'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 控制底部弹出层是否显示
    modalShow: false,
    shipinList: [],
    dianzanUrl: "",
    shipinDatas:'',//历史
    videoIndex:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this._loadshipinList()
     
    var shipinKey = 'cityshipin'
    var shipin = ''
    if(wx.getStorageSync(shipinKey)){
      shipin = wx.getStorageSync(shipinKey)
    }else{
      shipin = ''
      wx.setStorageSync(shipinKey, [])
    }
    console.log('shipinKey---',shipinKey)
    this.setData({
      shipinDatas:shipin,
    })
    console.log('搜索历史记录--',this.data.shipinDatas)


    // 小程序端调用云数据库
    /*  const db = wx.cloud.database()
     db.collection('shipin').orderBy('createTime', 'desc').get().then((res)=>{
       console.log(res)
       const data = res.data
       for (let i = 0, len = data.length; i<len; i++){
         data[i].createTime = data[i].createTime.toString()
       }
       this.setData({
         shipinList: data
       })
     }) */
  },
  videoPlay(event){
    var index = video.getDataSet(event, 'index');

    if (!this.data.videoIndex) { // 没有播放时播放视频
        this.setData({
            videoIndex: index
        })          
        var videoContext = wx.createVideoContext('video' + index)
        videoContext.play()
    } else {
        var videoContextPrev = wx.createVideoContext('video' + this.data.videoIndex)
        videoContextPrev.stop()
        this.setData({
            videoIndex: index
        })  
        var videoContextCurrent = wx.createVideoContext('video' + index)
        videoContextCurrent.play()
    }
},

  addshipin(obj){//添加历史记录
    var shipinname = 'cityshipin'//先定好key的名字shipinname
    var shipindata = wx.getStorageSync(shipinname) //和onload里一样取key
 
    //查看是否是重复搜索记录
    var shipinDatastr = JSON.stringify(shipindata)
    console.log('shipindata---',shipindata)
 
    if(shipinDatastr.indexOf(obj.id) == -1){//通过name字段判断是否重复
        if(shipindata.length < 10){ //只能存10条
          shipindata.push(obj)
        }else{//删掉多余10条的
          shipindata.shift()
          shipindata.push(obj)
        }
 
        wx.setStorageSync(shipinname, shipindata)//存到缓存中 shipindata就是存的内容
 
        this.setData({ // 把历史记录赋给事先定义好的变量就可以了
            shipinDatas:shipindata.reverse() //reverse() 起到倒序的作用，如果不加这个最新添加的历史记录在下面，所以我给前后顺序颠倒了一下
            //shipinDatas:shipindata
        })
 
    }
 
 
},
  onSearch(event) {
    // console.log(event.detail.keyword)
    this.setData({
      shipinList: []
    })
    keyword = event.detail.keyword
    this._loadshipinList(0)
  },

  _loadshipinList(start = 0) {
    wx.showLoading({
      title: '拼命加载中',
    })


    wx.cloud.callFunction({
      name: 'shipin',
      data: {
        keyword,
        start:0,
        count: 50,
        $url: 'lost',
      }
    }).then((res) => {
      for (var i = 0; i < res.result.length; i++) { 
        res.result[i].createTime = formatTime(new Date(res.result[i].createTime))
     }
      this.setData({
        shipinList: this.data.shipinList.concat(res.result)
      })
      wx.hideLoading()
      wx.stopPullDownRefresh()
    })
  },

  goComment(event) {
    var id = event.currentTarget.dataset.shipinid;//获取id
    //保存历史
    var shipinObj = {
      id:id,
    }
    this.addshipin(shipinObj) //调用存的方法，把值传进去
    wx.navigateTo({
      url: '/pages/index/shipin/shipin-nr/shipin-nr?shipinId=' + event.currentTarget.dataset.shipinid,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      shipinList: []
    })
    this._loadshipinList(0)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this._loadshipinList(this.data.shipinList.length)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(event) {
    console.log(event)
    let shipinObj = event.target.dataset.shipin
    return {
      title: shipinObj.content,
      path: `/pages/shipin/shipin?shipinId=${shipinObj._id}`,
      // imageUrl: ''
    }
  }
})